<template>
  <el-dialog title="必要参数" :modelValue="visiable" width="500px" @close="emit('closed')">
    <el-form :model="model" label-width="80px" :inline="false" v-if="model">
      <!-- 牵引绳 -->
      <el-form-item label="牵引绳">
        <el-select v-model="model.qianWire" placeholder="请选择">
          <el-option-group v-for="group in options" :key="group.label" :label="group.label">
            <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item">
              <span class="text-gray-700 font-normal">{{ item.label }}</span>
              <span class="float-right text-gray-400 font-normal">{{ item.value.wireWeight }}kg/m</span>
            </el-option>
          </el-option-group>
        </el-select>

        <div
          class="w-full flex justify-between items-center py-1 px-3 rounded-sm bg-[#2c3e50]"
          v-if="model.qianWire.value">
          <span class="text-xs text-gray-100">{{ model.qianWire.label }}</span>
          <span class="text-xs text-gray-100">{{ model.qianWire.value.wireWeight }}kg/m</span>
        </div>
      </el-form-item>

      <!-- 导引绳 -->
      <el-form-item label="导引绳">
        <el-select v-model="model.daoWire" placeholder="请选择">
          <el-option-group v-for="group in options" :key="group.label" :label="group.label">
            <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item">
              <span class="text-gray-700 font-normal">{{ item.label }}</span>
              <span class="float-right text-gray-400 font-normal">{{ item.value.wireWeight }}kg/m</span>
            </el-option>
          </el-option-group>
        </el-select>

        <div
          class="w-full flex justify-between items-center py-1 px-3 rounded-sm bg-[#2c3e50]"
          v-if="model.daoWire.value">
          <span class="text-xs text-gray-100">{{ model.daoWire.label }}</span>
          <span class="text-xs text-gray-100">{{ model.daoWire.value.wireWeight }}kg/m</span>
        </div>
      </el-form-item>

      <el-form-item label="摩阻系数">
        <el-input v-model="model.moZuXiSu" placeholder="请输入摩阻系数">
          <template #prepend>
            <ListNumbers theme="outline" size="18" fill="#333" />
          </template>
        </el-input>
      </el-form-item>

      <el-form-item label="展放数量">
        <el-input v-model="model.num" placeholder="请输入导线展放数量">
          <template #prepend>
            <Acoustic theme="outline" size="18" fill="#333" />
          </template>
          <template #suffix>
            <span class="text-xs">根</span>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item label="牵引方向">
        <el-select v-model="model.order" placeholder="请选择牵引方向">
          <el-option label="正向" value="true" />
          <el-option label="反向" value="false" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即计算</el-button>
        <el-button @click="emit('closed')">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import useCalculatePower from '@/composables/useCalculatePower'
import useWire from '@/composables/useWire'
import { Acoustic, ListNumbers } from '@icon-park/vue-next'
import { reactive, ref } from 'vue'

const { visiable, zoneId } = defineProps<{
  visiable: boolean
  zoneId: number
}>()
const emit = defineEmits(['closed'])

const { findallNoPage, wires } = useWire()
const { calcPower } = useCalculatePower()

const model = reactive({
  qianWire: { label: '', value: null } as unknown as WireOption,
  daoWire: { label: '', value: null } as unknown as WireOption,
  moZuXiSu: 1.015,
  num: 1,
  order: true,
})

interface WireOption {
  label: string
  value: WireModel
}
const options = ref<{ label: string; options: WireOption[] }[]>([])

async function initOptions() {
  await findallNoPage()
  const wireOptions = wires
    .value!.filter((wire) => wire.wireName.includes('引绳'))
    .map((wire) => ({ label: wire.wireStyle, value: wire }))
  const daoOptions = wires
    .value!.filter((wire) => wire.wireName.includes('导线'))
    .map((wire) => ({ label: wire.wireStyle, value: wire }))

  options.value.push({
    label: '导线',
    options: daoOptions,
  })
  options.value.push({
    label: '引绳',
    options: wireOptions,
  })
}

async function onSubmit() {
  await calcPower(
    {
      QianWireId: model.qianWire.value.id!,
      DaoWireId: model.daoWire.value.id!,
      e: model.moZuXiSu,
      num: model.num,
      order: model.order,
      isCalc: false,
      isSmallQianZhang: false,
    },
    zoneId,
  )
}

initOptions()
</script>

<style></style>
